# 基因突变

import random
import numpy as np
from encode import *


def mutation(pop, pm, actList, resources):
    chrome_length = len(pop[0].modeStr)
    pop_size = len(pop)
    for i in range(0, pop_size):
        if random.random() < pm:
            indiv = pop[i]
            # print("************************")
            # print("indiv.actStr: ", indiv.actStr)
            # print("indiv.modeStr: ", indiv.modeStr)
            # print("------------------------")
            # 活动变异
            apoint = random.randint(1, chrome_length - 1)
            anothor = chrome_length - apoint
            indiv.actStr[apoint], indiv.actStr[anothor] = indiv.actStr[anothor], indiv.actStr[apoint]
            indiv.actStr = fixActOrder(indiv.actStr, actList)
            # 模式变异
            mpoint = random.randint(1, chrome_length - 1)
            while indiv.modeStr[mpoint] == np.random.randint(1, 4):
                pass
            indiv.modeStr = fixModeOrder(indiv.modeStr, indiv.actStr, actList, indiv.resources)  # 剔除不可行变异

            #
            # print("indiv.actStr: ", indiv.actStr)
            # print("indiv.modeStr: ", indiv.modeStr)
        # print(i)
    return pop


if __name__ == '__main__':
    pop = [[[2, 1, 3, 5, 4, 6], [1, 2, 4, 6, 3, 5]], [[0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 0]]]
    mutationpro = 0.1
    chrome_length = 6
    p1 = mutation(pop, mutationpro, chrome_length)
    print(p1)
